package com.bookstore;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.ResourceBundle;

@WebServlet(urlPatterns = {"/RequestServlet01"})
public class RequestServlet01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String method = request.getMethod();
        //1、request.setCharacterEncoding("utf-8");
//        2、response.setHeader("Content-type","text/html;charset=utf-8");


        String username = request.getParameter("username");

//        3、username = new String(username.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
//        4.response.setContentType("test/html");
//          response.setCharacterEncoding("UTF-8");
        System.out.println(username);
//        String params=" ";
//        if("GET".equals(method)){
//            params=request.getQueryString();
//        }else if("POST".equals(method)){
//            BufferedReader reader = request.getReader();
//            params=reader.readLine();
//        }
//        System.out.println(params);
        request.getRequestDispatcher("/RequestServlet01").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Content-type","text/html;charset=utf-8");//设置响应头编码
        String username = request.getParameter("username");
        System.out.println(username);
        request.setAttribute("msg","Hello Word!");
//        response.setHeader("location","test.jsp");
//        response.setStatus(302);//重定向
//        request.getRequestDispatcher("test.jsp").forward(request,response);//可以实现资源的共享
//        response.sendRedirect("test.jsp");//使用重定向无法共享数据资源
        response.getWriter().write("Hello 你好！！！");
//        ServletOutputStream outputStream = response.getOutputStream();
//        outputStream.println();
}
}
